home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1997 #3 / Amiga Plus CD - 1997 - No. 03.iso / pd / demo-versionen / maxoncpp4-demo / demo / supercode / screen.c < prev    next >
C/C++ Source or Header  |  1996-12-31  |  9KB  |  397 lines

  1. //-------------------------------------
  2. //
  3. // SuperCode (c) 1996 by T.Kühn 
  4. //
  5. // Programmiersprache:    ANSI-C
  6. // Projektstart:            28.8.94
  7. //
  8. // Modul:                Screen-handling
  9. //
  10. //-------------------------------------
  11.  
  12. #include <pragma/intuition_lib.h>
  13. #include <pragma/dos_lib.h>
  14. #include <pragma/graphics_lib.h>
  15. #include <pragma/diskfont_lib.h>
  16. #include <pragma/gadtools_lib.h>
  17.  
  18. #include <Struct.h>
  19.  
  20. //-------------------------------------
  21. //-- SUBS ----------------------------
  22. VOID    Screen_Init(struct tkScreen *Scr);
  23. ULONG    Screen_Open(struct tkScreen *Scr);
  24. ULONG    Screen_Close(struct tkScreen *Scr);
  25.  
  26. //-------------------------------------
  27.  
  28. struct tkScreen Scrn;
  29.  
  30. WORD Pen_3D[]= { 1,0,1,2,1,3,1,0,4,1,2,-1 };
  31.  
  32. UBYTE PUB_NAME[]=    "SuperCode";
  33. UBYTE PUB_WB[]=        "Workbench";
  34.  
  35. UBYTE Col[MAX_COL][3]=
  36. {
  37.     255,    255,    255,
  38.  
  39.     255,    0,        0,
  40.     0,        0,        255,
  41.     0,        255,    0,
  42.     255,    255,    0,
  43.     0,        255,    255,
  44.     255,    0,        255,
  45.  
  46.     0,        0,        0,
  47.  
  48.     128,    0,        0,
  49.     0,        0,        128,
  50.     20,    168,    20,
  51.  
  52.     238,    150,    0,
  53.     0,        128,    128,
  54.     128,    0,        128,
  55.  
  56.     169,    119,    61,
  57. };
  58.  
  59. UWORD    Reg_Col[MAX_COL+2]=    {0};
  60.  
  61. //-------------------------------------
  62.  
  63.  
  64. //-------------------------------------
  65. VOID Font_Open(struct tkFont *tkfont)
  66. {
  67.     struct TextAttr *attr;
  68.     struct TextFont *font=tkfont->font;
  69.  
  70.     if (!font)
  71.     {
  72.         do    {
  73.                 attr=&tkfont->attr;
  74.                 font=OpenDiskFont(attr);
  75.                 if (!font)
  76.                 {
  77.                     font=OpenFont(attr);
  78.                     if (!font)
  79.                     {
  80.                         if (ASK_SELECT==display_error(ERR_FONT,ASK_EXIT|ASK_AGAIN|ASK_SELECT,attr->ta_Name,attr->ta_YSize))
  81.                         {
  82.                             prefs_askfont(attr);
  83.                         }
  84.                     }
  85.                 }
  86.             } while(!font);
  87.  
  88. //        Window_SetLimits();
  89.         tkfont->font=font;
  90.     }
  91. }
  92. //-------------------------------------
  93. VOID Font_Close(struct tkFont *tkfont)
  94. {
  95.     struct TextAttr *attr=&tkfont->attr;
  96.     struct TextFont *font=tkfont->font;
  97.  
  98.     if (font)
  99.     {
  100.         CloseFont(font);
  101.         tkfont->font=0;
  102.     }
  103. }
  104. //-------------------------------------
  105. VOID Font_Init(struct tkFont *tkfont,UBYTE *fontname,ULONG fontsize)
  106. {
  107.     struct TextAttr *attr=&tkfont->attr;
  108.  
  109.     Font_Close(tkfont);
  110.  
  111.     strncpy(tkfont->fontname,fontname,MAX_FONTNAME);
  112.  
  113.     attr->ta_Name=tkfont->fontname;
  114.     attr->ta_YSize=fontsize;
  115.  
  116.     Font_Open(tkfont);
  117. }
  118. //-------------------------------------
  119.  
  120. //-------------------------------------
  121. ULONG bit(ULONG c)
  122. {
  123.     ULONG r=0;
  124.  
  125.     r|=c;
  126.     r|=c<<8;
  127.     r|=c<<16;
  128.     r|=c<<24;
  129.  
  130.     return r;
  131. }
  132. //-------------------------------------
  133.  
  134. //-------------------------------------
  135. VOID Screen_Init(struct tkScreen *Scr)
  136. {
  137.     ULONG t;
  138.  
  139.     memset(Scr,0,sizeof(struct tkScreen));
  140.  
  141.     Scr->Width=prg_prefs->scr.scr_width;
  142.     Scr->Height=prg_prefs->scr.scr_height;
  143.     Scr->Depth=prg_prefs->scr.scr_depth;
  144.     Scr->Mode=prg_prefs->scr.scr_mode;
  145.     Scr->Public=prg_prefs->scr.scr_public;
  146.  
  147.     Scr->Scrn=0;
  148.     Scr->FlgOwn=FALSE;
  149.     Scr->FlgWB=FALSE;
  150.     Scr->FlgPublic=FALSE;
  151.     Scr->FlgPrivate=FALSE;
  152.  
  153.     for (t=0;t<MAX_COL;t++) Reg_Col[t]=-1;
  154.  
  155. }
  156. //-------------------------------------
  157. ULONG Screen_Open(struct tkScreen *Scr)
  158. {
  159.     BOOL scrn_chg=FALSE,font_chg=FALSE;
  160.     ULONG Err=0;
  161.     struct Screen *ScrPtr=0;
  162.  
  163.     if      (prg_prefs->scr.scr_width    !=Scr->Width )                scrn_chg=TRUE;
  164.     else if (prg_prefs->scr.scr_height    !=Scr->Height)                scrn_chg=TRUE;
  165.     else if (prg_prefs->scr.scr_depth    !=Scr->Depth)                scrn_chg=TRUE;
  166.     else if (prg_prefs->scr.scr_mode        !=Scr->Mode)                scrn_chg=TRUE;
  167.     else if (prg_prefs->scr.scr_overscan!=Scr->OverScan)            scrn_chg=TRUE;
  168.     else if (prg_prefs->scr.scr_public    !=Scr->Public)                scrn_chg=TRUE;
  169.     else if (0!=strcmp(Scr->PubName,prg_prefs->scr.scr_pubname)) scrn_chg=TRUE;
  170.  
  171.     if (0!=strcmp(Scr->tkfont.fontname,prg_prefs->scr.fontname))    font_chg=TRUE;
  172.     else if (Scr->tkfont.attr.ta_YSize!=prg_prefs->scr.fontattr.ta_YSize) font_chg=TRUE;
  173.  
  174.     if (Scr && (scrn_chg || font_chg))
  175.     {
  176. //        Window_ScreenDown();
  177.         Screen_Close(Scr);
  178.  
  179.         Scr->Width=prg_prefs->scr.scr_width;
  180.         Scr->Height=prg_prefs->scr.scr_height;
  181.         Scr->Depth=prg_prefs->scr.scr_depth;
  182.         Scr->Mode=prg_prefs->scr.scr_mode;
  183.         Scr->Public=prg_prefs->scr.scr_public;
  184.         Scr->OverScan=prg_prefs->scr.scr_overscan;
  185.  
  186.         if (font_chg)
  187.         {
  188.             Font_Init(&Scr->tkfont,prg_prefs->scr.fontname,prg_prefs->scr.fontattr.ta_YSize);
  189.         }
  190.  
  191. //        if (!ScrPtr && Scr->Private)            // privaten Screen mitbenutzen
  192. //        {
  193. //            printf("patch private Screen\n");
  194. //        }
  195.  
  196.         if (!ScrPtr && Scr->Public)            // public Screen mitbenutzen (WB)
  197.         {
  198.             do    {
  199.                     StrCopy(Scr->PubName,prg_prefs->scr.scr_pubname,MAX_PUBSCREENNAME);
  200.                     ScrPtr=LockPubScreen(Scr->PubName);
  201.                     if (!ScrPtr)
  202.                     {
  203.                         display_error(ERR_SCRNPUB,ASK_EXIT|ASK_AGAIN,Scr->PubName);
  204.  
  205. //                        if (ASK_SELECT==display_error(ERR_SCRNPUB,ASK_EXIT|ASK_AGAIN|ASK_SELECT,Scr->PubName))
  206. //                        {
  207. //                            prefs_askpubscreen(prg_prefs->scr_pubname,MAX_PUBSCREENNAME);
  208. //                        }
  209.                     }
  210.                 }    while(!ScrPtr);
  211.  
  212.             Scr->FlgWB= (0==strcmp(Scr->PubName,PUB_WB)) ? TRUE : FALSE;
  213.  
  214.             ScreenToFront(ScrPtr);
  215.             Scr->FlgPublic=TRUE;
  216.         }
  217.  
  218.         if (!ScrPtr && Scr->Mode==LIKE_WORKBENCH)
  219.         {
  220. //            printf("open wb-like screen\n");
  221.  
  222.             do    {    ScrPtr=OpenScreenTags(0,
  223.                         SA_LikeWorkbench,TRUE,
  224.                         SA_Title,CatStr(TXT_SCRTITLE),
  225.                         SA_AutoScroll,TRUE,
  226.                         SA_PubName,PUB_NAME,
  227.                         SA_ErrorCode,&Err,
  228.                         SA_Interleaved,TRUE,
  229.                         SA_Font,&Scr->tkfont.attr,
  230.                         (Scr->Depth!=0)?(SA_Depth):(TAG_IGNORE),Scr->Depth,
  231.                         TAG_END);
  232.                 }    while((!ScrPtr) && ASK_AGAIN==display_error(ERR_SCRNLIKEWB,ASK_EXIT|ASK_AGAIN,Scr->Depth));
  233.  
  234.             if (ScrPtr) Scr->FlgOwn=TRUE;
  235.         }
  236.         if (!ScrPtr && Scr->Mode)                // eigenen Screen öffnen
  237.         {
  238. //            printf("open own screen ID:%X\n",Scr->Mode);
  239.  
  240.             do    {    ScrPtr=OpenScreenTags(0,
  241.                         SA_Width,Scr->Width,
  242.                         SA_Height,Scr->Height,
  243.                         SA_Depth,Scr->Depth,
  244.                         SA_Title,CatStr(TXT_SCRTITLE),
  245.                         SA_PubName,PUB_NAME,
  246.                         SA_DisplayID,Scr->Mode,
  247.                         SA_Overscan,Scr->OverScan,
  248.                         SA_AutoScroll,TRUE,
  249.                         SA_Pens,(ULONG)Pen_3D,
  250.                         SA_ErrorCode,&Err,
  251.                         SA_Interleaved,TRUE,
  252.                         SA_Font,&Scr->tkfont.attr,
  253.                         TAG_END);
  254.         
  255.                     if (!ScrPtr)
  256.                     {
  257.                         if (ASK_SELECT==display_error(ERR_SCRNOWN,ASK_EXIT|ASK_AGAIN|ASK_SELECT,Scr->Width,Scr->Height,Scr->Depth))
  258.                         {
  259.                             if (prefs_askscreen())
  260.                             {
  261.                                 Scr->Width=prg_prefs->scr.scr_width;
  262.                                 Scr->Height=prg_prefs->scr.scr_height;
  263.                                 Scr->Depth=prg_prefs->scr.scr_depth;
  264.                                 Scr->Mode=prg_prefs->scr.scr_mode;
  265.                                 Scr->OverScan=prg_prefs->scr.scr_overscan;
  266.                             }
  267.                         }
  268.                     }
  269.                 }    while(!ScrPtr);
  270.  
  271.             Scr->FlgOwn=TRUE;
  272.         }
  273.  
  274.         if (ScrPtr)
  275.         {
  276.             LONG rx,ry,s;
  277.  
  278.             Scr->Scrn=ScrPtr;
  279.             Scr->Visual=GetVisualInfoA(ScrPtr,TAG_END);
  280.             Scr->DrawInfo=GetScreenDrawInfo(ScrPtr);
  281.  
  282.             rx=Scr->DrawInfo->dri_Resolution.X;
  283.             ry=Scr->DrawInfo->dri_Resolution.Y;
  284.  
  285.             Scr->Xres=rx;
  286.             Scr->Yres=ry;
  287.  
  288.             if (rx<ry)            { rx=ry/rx;ry=1; }
  289.             else if (rx>ry)    { ry=rx/ry;rx=1; }
  290.             else                    { rx=1;ry=1; }
  291.  
  292.             Scr->Xmul=rx;
  293.             Scr->Ymul=ry;
  294.  
  295.             if (Scr->FlgOwn) PubScreenStatus(ScrPtr,0);
  296.  
  297.             if (Scr->FlgWB)
  298.             {
  299.                 ULONG t;
  300.                 ULONG d=1<<ScrPtr->RastPort.BitMap->Depth;
  301.  
  302.                 if (GfxBase->lib_Version>=39 && ( d > 8))
  303.                 {
  304.                     for (t=0;t<MAX_COL;t++)
  305.                     {
  306.                         Reg_Col[t] = ObtainBestPen(ScrPtr->ViewPort.ColorMap,bit(Col[t][0]),bit(Col[t][1]),bit(Col[t][2]),
  307.                             OBP_Precision,PRECISION_IMAGE,
  308.                             OBP_FailIfBad,TRUE,
  309.                             TAG_END);
  310.     
  311.     //                    Reg_Col[t] = ObtainPen(ScrPtr->ViewPort.ColorMap,-1,bit(Col[t][0]),bit(Col[t][1]),bit(Col[t][2]),0);
  312.     
  313.     
  314.                         for (s=0;s<t;s++)
  315.                         {
  316.                             if (Reg_Col[s]==Reg_Col[t]) Reg_Col[t]=0xffff;
  317.                         }
  318.     
  319.                         if (Reg_Col[t]==0xffff) {break;}
  320.                     }
  321.                     Reg_Col[t]=-1;
  322.                 }
  323.                 else
  324.                 {
  325.                     for (t=0;t+1<d;t++)
  326.                     {
  327.                         Reg_Col[t] = t+1;
  328.                     }
  329.                     Reg_Col[t]=-1;
  330.                 }
  331.  
  332.             }
  333.         }
  334.         Menu_Layout();
  335. //        Window_ScreenUp();
  336.     }
  337.     return(Err)
  338. }
  339. //-------------------------------------
  340. ULONG Screen_Close(struct tkScreen *Scr)
  341. {
  342.     ULONG Err=0;
  343.     struct Screen *Scrn=0;
  344.  
  345.     if (Scr)
  346.     {
  347.         Scrn=Scr->Scrn;
  348.         if (Scrn)
  349.         {
  350.             ULONG t;
  351.  
  352.             if (GfxBase->lib_Version>=39)
  353.             {
  354.                 for (t=0;t<MAX_COL;t++)
  355.                 {
  356.                     if (Reg_Col[t]!=-1) ReleasePen(Scrn->ViewPort.ColorMap,Reg_Col[t]);
  357.                     Reg_Col[t]=-1;
  358.                 }
  359.             }
  360.             if (Scr->Visual)
  361.             {
  362.                 FreeVisualInfo(Scr->Visual);
  363.                 Scr->Visual=0;
  364.             }
  365.             if (Scr->DrawInfo)
  366.             {
  367.                 FreeScreenDrawInfo(Scrn,Scr->DrawInfo);
  368.                 Scr->DrawInfo=0;
  369.             }
  370.     
  371.             if (Scr->FlgOwn)
  372.             {
  373.     //            printf("remove own Screen\n");
  374.     
  375.                 while(TRUE)
  376.                 {
  377.                     if (CloseScreen(Scrn)) break;
  378.                     Delay(25);
  379.                 }
  380.             }
  381.             else if (Scr->FlgPublic)
  382.             {
  383.     //            printf("free PubScreen\n");
  384.                 UnlockPubScreen(0,Scrn);
  385.             }
  386.         }
  387.         Scr->Scrn=0;
  388.         Scr->FlgWB=FALSE;
  389.         Scr->FlgOwn=FALSE;
  390.         Scr->FlgPublic=FALSE;
  391.         Scr->FlgPrivate=FALSE;
  392.     }
  393.     return(Err)
  394. }
  395. //-------------------------------------
  396.  
  397.